package cn.edu.lanqiao.control;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.lanqiao.entity.UserContactWays;
import cn.edu.lanqiao.entity.UserEntity;
import cn.edu.lanqiao.service.IUserInfoService;
import cn.edu.lanqiao.utils.MyUtils;

@Controller
public class UserInfoController {

	// 组合
	@Resource
	private IUserInfoService userInfoService;

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String doLogin(String textName, Model model) {
		System.out.println(textName + "正在请求登录。。。。");
		// 从数据库获取对象
		// UserEntity
		// userE=userInfoService.selectById(MyUtils.obj2Int(textName));
		UserEntity userE = userInfoService.selectByCode(textName);
		// 判定
		String strName = (userE == null ? "" : userE.getUsercode());
		String strSex = (userE == null ? "" : userE.getSex());
		System.out.println("数据库查询用户信息" + strName + "--性别：" + strSex);
		// 放入缓存
		model.addAttribute("logger", strName);
		return "logined";
	}

	@RequestMapping("/login1")
	public ModelAndView doLogin1(UserEntity user) {
		// 从前台表单获取的值
		System.out.println(user.getUsercode() + "正在请求登录。。。。");
		// 从数据库获取对象
		// UserEntity
		// userE=userInfoService.selectById(MyUtils.obj2Int(textName));
		UserEntity userE = userInfoService.selectByCode(user.getUsercode());
		// 判定
		String strName = (userE == null ? "" : userE.getUsercode());
		String strSex = (userE == null ? "" : userE.getSex());
		System.out.println("数据库查询用户信息" + strName + "--性别：" + strSex);
		ModelAndView model = new ModelAndView();
		model.setViewName("logined");
		// 查询结果放入缓存
		model.addObject("logger", strName);
		return model;
	}

	@RequestMapping("/login2")
	public ModelAndView doLogin2(UserEntity user) {
		// 从前台表单获取的值
		System.out.println(user.getUsercode() + "正在请求登录。。。。");
		System.out.println("从表单获取的联系电话：" + user.getUserContactWays().getTellphone());
		// 从数据库获取对象
		// UserEntity
		// userE=userInfoService.selectById(MyUtils.obj2Int(textName));
		UserEntity userE = userInfoService.selectByCode(user.getUsercode());
		// 判定
		String strName = (userE == null ? "" : userE.getUsercode());
		String strSex = (userE == null ? "" : userE.getSex());
		System.out.println("数据库查询用户信息：姓名" + strName + "--性别：" + strSex + "--电话：" + userE.getTellphone());
		ModelAndView model = new ModelAndView();
		model.setViewName("logined");
		// 查询结果放入缓存
		model.addObject("logger", strName);
		return model;
	}

	// list测试
	@RequestMapping("/login3")
	public ModelAndView doLogin3(UserEntity user) {
		// 从前台表单获取的值
		System.out.println(user.getUsercode() + "正在请求登录。。。。");
		List<UserContactWays> lstUserC = user.getLstUserContactWays();
		// 遍历前台的list值
		if (lstUserC != null && lstUserC.size() > 0) {
			for (int i = 0; i < lstUserC.size(); i++) {
				// 循环打印
				System.out.println("从表单获取的联系电话" + i + "：" + lstUserC.get(i).getTellphone());
			}
		} else {
			System.out.println("从表单获取的联系电话失败。。。");
		}
		// 从数据库获取对象
		UserEntity userE = userInfoService.selectByCode(user.getUsercode());
		// 判定
		String strName = (userE == null ? "" : userE.getUsercode());
		String strSex = (userE == null ? "" : userE.getSex());
		System.out.println("数据库查询用户信息：姓名" + strName + "--性别：" + strSex + "--电话：" + userE.getTellphone());
		ModelAndView model = new ModelAndView();
		model.setViewName("logined");
		// 查询结果放入缓存
		model.addObject("logger", strName);
		return model;
	}

	// map测试
	@RequestMapping("/login4")
	public ModelAndView doLogin4(UserEntity user) {
		// 从前台表单获取的值
		System.out.println(user.getUsercode() + "正在请求登录呢。。。。");
		Map<String, UserContactWays> mapC = user.getMapUserContactWays();
		System.out.println("从表单中获取的电话：");
		if (mapC != null && mapC.size() > 0) {
			// 依次打印
			System.out.println(mapC.get("tel1") == null ? "--1没有获取到" : mapC.get("tel1").getTellphone());
			System.out.println(mapC.get("tel2") == null ? "--2没有获取到" : mapC.get("tel2").getTellphone());

		}
		// 从数据库获取对象
		UserEntity userE = userInfoService.selectByCode(user.getUsercode());
		// 判定
		String strName = (userE == null ? "" : userE.getUsercode());
		System.out.println("数据库查询用户信息：姓名" + strName + "--性别：" + userE.getSex() + "--电话：" + userE.getTellphone());
		ModelAndView model = new ModelAndView();
		model.setViewName("logined");
		// 查询结果放入缓存
		model.addObject("logger", strName);
		return model;
	}

	// set测试
	@RequestMapping("/login5")
	public ModelAndView doLogin5(UserEntity user) {
		// 从前台表单获取的值
		System.out.println(user.getUsercode() + "正在请求登录呢。。。。");
		Set<UserContactWays> setC = user.getSetUserContactWays();
		if (setC != null && setC.size() > 0) {
            //循环遍历
			for (UserContactWays userEC : setC) {

				System.out.println("从表单中获取的电话："+userEC==null?"":userEC.getTellphone());
			}
		}
		// 从数据库获取对象
		UserEntity userE = userInfoService.selectByCode(user.getUsercode());
		// 判定
		String strName = (userE == null ? "" : userE.getUsercode());
		System.out.println("数据库查询用户信息：姓名" + strName + "--性别：" + userE.getSex() + "--电话：" + userE.getTellphone());
		ModelAndView model = new ModelAndView();
		model.setViewName("logined");
		// 查询结果放入缓存
		model.addObject("logger", strName);
		return model;
	}
	
	//@RequestParam 注解测试
	@RequestMapping(value = "/login6", method = RequestMethod.POST)
	public String doLogin6(String textName, @RequestParam(value="textAge")String userAge,Model model) {
		System.out.println(textName + "正在请求登录。。。。");
		
		System.out.println("表单textName--"+textName);
		System.out.println( "表单年龄textAge--"+userAge);

		// 从数据库获取对象
		// userE=userInfoService.selectById(MyUtils.obj2Int(textName));
		UserEntity userE = userInfoService.selectByCode(textName);
		// 判定
		String strName = (userE == null ? "" : userE.getUsercode());
		String strSex = (userE == null ? "" : userE.getSex());
		System.out.println("数据库查询用户信息" + strName + "--性别：" + strSex);
		// 放入缓存
		model.addAttribute("logger", strName);
		return "logined";
	}


}
